//
//  WNAppManager.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit
import AdSupport
import KeychainAccess
import Kingfisher

/// 获取应用名
func getAppName() -> String {
    let dic = Bundle.main.infoDictionary!
    let nameVal = dic["CFBundleDisplayName"] as? String
    return nameVal ?? ""
}

/// 获取应用简称
func getAppShortName() -> String {
    let dic = Bundle.main.infoDictionary!
    let nameVal = dic["CFBundleDisplayShortName"] as? String
    return nameVal ?? ""
}

/// 获取APP版本
func getAPPVersion() -> String {
    return Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
}

/// 获取设备系统版本
func getDeviceVersion() -> String {
    return UIDevice.current.systemVersion
}

/// 获取UUID
func getUUID() -> String {
    return UIDevice.current.identifierForVendor?.uuidString ?? ""
}

/// 获取IDFA
func getIDFA() -> String {
    return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}

/// 获取ID地址
func getIpAddress() -> String {
    var addresses = [String]()
    var ifaddr : UnsafeMutablePointer<ifaddrs>? = nil
    if getifaddrs(&ifaddr) == 0 {
        var ptr = ifaddr
        while (ptr != nil) {
            let flags = Int32(ptr!.pointee.ifa_flags)
            var addr = ptr!.pointee.ifa_addr.pointee
            if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
                if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
                    var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                    if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                        if let address = String(validatingUTF8:hostname) {
                            addresses.append(address)
                        }
                    }
                }
            }
            ptr = ptr!.pointee.ifa_next
        }
        freeifaddrs(ifaddr)
    }
    return addresses.first ?? "0.0.0.0"
}

/// 获取顶部状态栏高度
func getStatusBarHeight() -> CGFloat {
    var h: CGFloat = 0.0
    if #available(iOS 13.0, *) {
        h = WNSBManager?.statusBarFrame.height ?? 0.0
    }else {
        h = UIApplication.shared.statusBarFrame.height
    }
    return h
}

/// 获取底部安全距离
func getSafeTabBarTotalHeight() -> CGFloat {
    var b: CGFloat = 0.0
    if #available(iOS 13.0, *) {
        b = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows.first?.safeAreaInsets.bottom ?? 0.0
    }else {
        b = UIApplication.shared.statusBarFrame.height
    }
    return b
}

/// 获取手机机型
func getPhoneModel() -> String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let platform = withUnsafePointer(to: &systemInfo.machine.0) { ptr in
        return String(cString: ptr)
    }
    if platform == "iPhone3,1" { return "iPhone 4"}
    if platform == "iPhone3,2" { return "iPhone 4"}
    if platform == "iPhone3,3" { return "iPhone 4"}
    if platform == "iPhone4,1" { return "iPhone 4S"}
    if platform == "iPhone5,1" { return "iPhone 5"}
    if platform == "iPhone5,2" { return "iPhone 5"}
    if platform == "iPhone5,3" { return "iPhone 5C"}
    if platform == "iPhone5,4" { return "iPhone 5C"}
    if platform == "iPhone6,1" { return "iPhone 5S"}
    if platform == "iPhone6,2" { return "iPhone 5S"}
    if platform == "iPhone7,1" { return "iPhone 6 Plus"}
    if platform == "iPhone7,2" { return "iPhone 6"}
    if platform == "iPhone8,1" { return "iPhone 6S"}
    if platform == "iPhone8,2" { return "iPhone 6S Plus"}
    if platform == "iPhone8,4" { return "iPhone SE"}
    if platform == "iPhone9,1" { return "iPhone 7"}
    if platform == "iPhone9,2" { return "iPhone 7 Plus"}
    if platform == "iPhone10,1" { return "iPhone 8"}
    if platform == "iPhone10,4" { return "iPhone 8"}
    if platform == "iPhone10,2" { return "iPhone 8 Plus"}
    if platform == "iPhone10,5" { return "iPhone 8 Plus"}
    if platform == "iPhone10,3" { return "iPhone X"}
    if platform == "iPhone10,6" { return "iPhone X"}
    if platform == "iPhone11,8" { return "iPhone XR"}
    if platform == "iPhone11,2" { return "iPhone XS"}
    if platform == "iPhone11,4" { return "iPhone XS Max"}
    if platform == "iPhone11,6" { return "iPhone XS Max"}
    if platform == "iPhone12,1" { return "iPhone 11"}
    if platform == "iPhone12,3" { return "iPhone 11 Pro"}
    if platform == "iPhone12,5" { return "iPhone 11 Pro Max"}
    if platform == "iPhone12,8" { return "iPhone SE 2"}
    if platform == "iPhone13,1" { return "iPhone 12 mini"}
    if platform == "iPhone13,2" { return "iPhone 12"}
    if platform == "iPhone13,3" { return "iPhone 12 Pro"}
    if platform == "iPhone13,4" { return "iPhone 12 Pro Max"}
    if platform == "iPhone14,4" { return "iPhone 13 mini"}
    if platform == "iPhone14,5" { return "iPhone 13"}
    if platform == "iPhone14,2" { return "iPhone 13 Pro"}
    if platform == "iPhone14,3" { return "iPhone 13 Pro Max"}
    if platform == "iPhone14,6" { return "iPhone SE 3"}
    if platform == "iPhone14,7" { return "iPhone 14"}
    if platform == "iPhone14,8" { return "iPhone 14 Plus"}
    if platform == "iPhone15,2" { return "iPhone 14 Pro"}
    if platform == "iPhone15,3" { return "iPhone 14 Pro Max"}
    if platform == "iPhone15,4" { return "iPhone 15"}
    if platform == "iPhone15,5" { return "iPhone 15 Plus"}
    if platform == "iPhone16,1" { return "iPhone 15 Pro"}
    if platform == "iPhone16,2" { return "iPhone 15 Pro Max"}
    if platform == "iPhone17,3" { return "iPhone 16"}
    if platform == "iPhone17,4" { return "iPhone 16 Plus"}
    if platform == "iPhone17,1" { return "iPhone 16 Pro"}
    if platform == "iPhone17,2" { return "iPhone 16 Pro Max"}

    if platform == "iPad1,1" { return "iPad"}
    if platform == "iPad2,1" { return "iPad_2"}
    if platform == "iPad2,2" { return "iPad_2"}
    if platform == "iPad2,3" { return "iPad_2"}
    if platform == "iPad2,4" { return "iPad_2"}
    if platform == "iPad3,1" { return "iPad_3_(WiFi)"}
    if platform == "iPad3,2" { return "iPad_3_(GSM+CDMA)"}
    if platform == "iPad3,3" { return "iPad_3"}
    if platform == "iPad3,4" { return "iPad_4_(WiFi)"}
    if platform == "iPad3,5" { return "iPad_4"}
    if platform == "iPad3,6" { return "iPad_4_(GSM+CDMA)"}
    if platform == "iPad6,11" { return "iPad_5_(WiFi)"}
    if platform == "iPad6,12" { return "iPad_5_(GSM+CDMA)"}
    if platform == "iPad7,5" { return "iPad_6_(WiFi)"}
    if platform == "iPad7,7" { return "iPad_6_(GSM+CDMA)"}
    if platform == "iPad7,11" { return "iPad_7_(WiFi)"}
    if platform == "iPad7,12" { return "iPad_7_(GSM+CDMA)"}
    if platform == "iPad11,6" { return "iPad_8_(WiFi)"}
    if platform == "iPad11,7" { return "iPad_8_(GSM+CDMA)"}
    if platform == "iPad12,2" { return "iPad_9_(WiFi)"}
    if platform == "iPad12,2" { return "iPad_9_(GSM+CDMA)"}
    if platform == "iPad13,18" { return "iPad (10th generation)"}
    if platform == "iPad13,19" { return "iPad (10th generation)"}
    
    if platform == "iPad4,1" { return "iPad_Air_(WiFi)"}
    if platform == "iPad4,2" { return "iPad_Air"}
    if platform == "iPad4,3" { return "iPad_Air_(Cellular)"}
    if platform == "iPad5,3" { return "iPad_Air_2"}
    if platform == "iPad5,4" { return "iPad_Air_2"}
    if platform == "iPad11,3" { return "iPad_Air_3"}
    if platform == "iPad11,4" { return "iPad_Air_3"}
    if platform == "iPad13,3" { return "iPad_Air_4"}
    if platform == "iPad13,2" { return "iPad_Air_4"}
    if platform == "iPad13,16" { return "iPad_Air_5"}
    if platform == "iPad13,17" { return "iPad_Air_5"}
    if platform == "iPad14,8" { return "iPad Air 11-inch (M2)"}
    if platform == "iPad14,9" { return "iPad Air 11-inch (M2)"}
    if platform == "iPad14,10" { return "iPad Air 13-inch (M2)"}
    if platform == "iPad14,11" { return "iPad Air 13-inch (M2)"}
    
    if platform == "iPad6,3" { return "iPad_Pro_9.7"}
    if platform == "iPad6,4" { return "iPad_Pro_9.7"}
    if platform == "iPad6,7" { return "iPad_Pro_12.9"}
    if platform == "iPad6,8" { return "iPad_Pro_12.9"}
    if platform == "iPad7,1" { return "iPad_Pro_12.9_2"}
    if platform == "iPad7,2" { return "iPad_Pro_12.9_2"}
    if platform == "iPad7,3" { return "iPad_Pro_10.5"}
    if platform == "iPad7,4" { return "iPad_Pro_10.5"}
    if platform == "iPad8,1" { return "iPad_Pro_11"}
    if platform == "iPad8,2" { return "iPad_Pro_11"}
    if platform == "iPad8,3" { return "iPad_Pro_11"}
    if platform == "iPad8,4" { return "iPad_Pro_11"}
    if platform == "iPad8,5" { return "iPad_Pro_12.9_3"}
    if platform == "iPad8,6" { return "iPad_Pro_12.9_3"}
    if platform == "iPad8,7" { return "iPad_Pro_12.9_3"}
    if platform == "iPad8,8" { return "iPad_Pro_12.9_3"}
    if platform == "iPad8,9" { return "iPad_Pro_11_2"}
    if platform == "iPad8,10" { return "iPad_Pro_11_2"}
    if platform == "iPad8,11" { return "iPad_Pro_12.9_4"}
    if platform == "iPad8,12" { return "iPad_Pro_12.9_4"}
    if platform == "iPad13,4" { return "iPad_Pro_11_3"}
    if platform == "iPad13,5" { return "iPad_Pro_11_3"}
    if platform == "iPad13,6" { return "iPad_Pro_11_3"}
    if platform == "iPad13,7" { return "iPad_Pro_11_3"}
    if platform == "iPad13,8" { return "iPad_Pro_12.9_5"}
    if platform == "iPad13,9" { return "iPad_Pro_12.9_5"}
    if platform == "iPad13,10" { return "iPad_Pro_12.9_5"}
    if platform == "iPad16,3" { return "iPad Pro 11-inch (M4)"}
    if platform == "iPad16,4" { return "iPad Pro 11-inch (M4)"}
    if platform == "iPad16,5" { return "iPad Pro 13-inch (M4)"}
    if platform == "iPad16,6" { return "iPad Pro 13-inch (M4)"}
    if platform == "iPad13,11" { return "iPad_Pro_12.9_5"}
    
    if platform == "iPad2,5" { return "iPad_Mini_(WiFi)"}
    if platform == "iPad2,6" { return "iPad_Mini"}
    if platform == "iPad2,7" { return "iPad_Mini_(GSM+CDMA)"}
    if platform == "iPad4,4" { return "iPad_Mini_2_(WiFi)"}
    if platform == "iPad4,5" { return "iPad_Mini_2_(Cellular)"}
    if platform == "iPad4,6" { return "iPad_Mini_2"}
    if platform == "iPad4,7" { return "iPad_Mini_3"}
    if platform == "iPad4,8" { return "iPad_Mini_3"}
    if platform == "iPad4,9" { return "iPad_Mini_3"}
    if platform == "iPad5,1" { return "iPad_Mini_4_(WiFi)"}
    if platform == "iPad5,2" { return "iPad_Mini_4_(LTE)"}
    if platform == "iPad11,1" { return "iPad_Mini_5"}
    if platform == "iPad11,2" { return "iPad_Mini_5"}
    if platform == "iPad14,1" { return "iPad_Mini_6"}
    if platform == "iPad14,2" { return "iPad_Mini_6"}
    
    if platform == "i386" { return "iPhone Simulator"}
    if platform == "x86_64" { return "iPhone Simulator"}
    return platform
}

func getLanguage() -> String {
    let language: String = NSLocale.preferredLanguages.first ?? ""
    switch language {
    case "zh-Hans", "zh-Hans-CN", "zh-Hans-HK", " zh-Hans-MO", "zh-Hans-TW", "zh-Hans-SG", "yue-Hans-CN":
        return "en-us" //中文简体
    case "zh-Hant-CN", "zh-Hant-HK", "zh-Hant-MO", "zh-Hant-TW", "zh-Hant-SG", "yue-Hant-CN":
        return "zh-tw" //中文繁体
    default:
        return language
    }
}

/// 获取分辨率
func getPhoneResolution() -> String {
    return "\(wn_toInt(ScreenW*Screen.scale))X\(wn_toInt(ScreenH*Screen.scale))"
}

/// 计算缓存的大小
func wn_fileSizeOfCache()-> Int {
    // 取出文件夹下所有文件数组
    let fileArr = FileManager.default.subpaths(atPath: CACHES_PATH)
    var size = 0
    if let arr = fileArr {
        //快速枚举出所有文件名 计算文件大小
        for file in arr {
            // 把文件名拼接到路径中
            let path = CACHES_PATH.appending("/\(file)")
            // 取出文件属性
            do {
                let floder = try FileManager.default.attributesOfItem(atPath: path)
                // 用元组取出文件大小属性
                for (abc, bcd) in floder {
                    // 累加文件大小
                    if abc == FileAttributeKey.size {
                        size += (bcd as AnyObject).integerValue
                    }
                }
            }catch {
                
            }
        }
        let mm = size / 1024 / 1024
        return mm
    }else {
        return 0
    }
}

/// 刪除缓存
func wn_clearCache() {
    // 取出文件夹下所有文件数组
    let fileArr = FileManager.default.subpaths(atPath: CACHES_PATH)
    // 遍历刪除
    if let arr = fileArr {
        for file in arr {
            let path = CACHES_PATH.appending("/\(file)")
            if FileManager.default.fileExists(atPath: path) {
                do {
                    try FileManager.default.removeItem(atPath: path)
                }catch {
                    
                }
            }
        }
    }
}


/// 获取🔑UserId
func getKeyUserId() -> String {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    let userId = try? keychain.getString(WNConfigure.Keychain.userId)
    return userId ?? ""
}

/// 存🔑UserId
func setKeyUserId(_ userId: String) {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    try? keychain.set(userId, key: WNConfigure.Keychain.userId)
}

/// 获取🔑登录用户订单Id
func getUserIdOrderId() -> String {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    let orderId = try? keychain.getString(WNConfigure.Keychain.userIdOrderId)
    return orderId ?? ""
}

/// 存🔑登录用户订单Id
func setUserIdOrderId(_ orderId: String) {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    try? keychain.set(orderId, key: WNConfigure.Keychain.userIdOrderId)
}

/// 获取🔑登录用户订阅订单Id
func getUserIdSubOrderId() -> String {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    let orderId = try? keychain.getString(WNConfigure.Keychain.userIdSubOrderId)
    return orderId ?? ""
}

/// 存🔑登录用户订阅订单Id
func setUserIdSubOrderId(_ orderId: String) {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    try? keychain.set(orderId, key: WNConfigure.Keychain.userIdSubOrderId)
}

/// 获取🔑UUId
func getKeyUUId() -> String {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    let uuid = try? keychain.getString(WNConfigure.Keychain.uuid)
    return uuid ?? ""
}

/// 存🔑UUId
func setKeyUUId(_ uuid: String) {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    try? keychain.set(uuid, key: WNConfigure.Keychain.uuid)
}

/// 获取🔑未登录用户订单Id
func getUuidOrderId() -> String {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    let orderId = try? keychain.getString(WNConfigure.Keychain.uuidOrderId)
    return orderId ?? ""
}

/// 存🔑未登录用户订单Id
func setUuidOrderId(_ orderId: String) {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    try? keychain.set(orderId, key: WNConfigure.Keychain.uuidOrderId)
}

/// 获取🔑未登录用户订单Id
func getUuidSubOrderId() -> String {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    let orderId = try? keychain.getString(WNConfigure.Keychain.uuidSubOrderId)
    return orderId ?? ""
}

/// 存🔑未登录用户订阅订单Id
func setUuidSubOrderId(_ orderId: String) {
    let keychain = Keychain(service: WNConfigure.Keychain.serviceName)
    try? keychain.set(orderId, key: WNConfigure.Keychain.uuidSubOrderId)
}

func isDark() -> Bool {
    return getDarkMode() == 1 ? true : false
}
